package attack.arfok;

import java.awt.geom.Rectangle2D;

import org.newdawn.slick.Animation;
import org.newdawn.slick.geom.Rectangle;

import framework.BasicFighter;
import framework.Util;
import framework.attack.AnimationAttack;

public class Tornado extends AnimationAttack {
	
	/** The time in ms when it last hits */
	protected int lastHit;
	
	/** */
	protected final static int WIND_UP = 350;
	
	protected int windUp;
	
	public Tornado(BasicFighter attacker) {
		super(attacker);
		this.spriteSheet = attacker.getAnimations().getSpriteSheet();
		this.attackAnimation = new Animation();
		this.attackAnimation.addFrame(this.spriteSheet.getSprite(3, 3), 150);
		this.attackAnimation.addFrame(this.spriteSheet.getSprite(4, 3), 100);
		this.attackAnimation.addFrame(this.spriteSheet.getSprite(0, 4), 100);
		this.attackAnimation.addFrame(this.spriteSheet.getSprite(1, 4), 100);
		this.attackAnimation.addFrame(this.spriteSheet.getSprite(4, 3), 100);
		this.attackAnimation.addFrame(this.spriteSheet.getSprite(0, 4), 100);
		this.attackAnimation.addFrame(this.spriteSheet.getSprite(1, 4), 100);
		this.attackAnimationBackward = Util.getFlippedAnimation(this.attackAnimation, true, false);
		this.isJumpingAuthorized = true;
	}
	
	@Override
	public Rectangle getHitBox() {
		Rectangle hitBox, att;
		int width, // attack width
		   height, // attack height
		  hitboxY, // x position of the hitbox
		  hitboxX; // y position of the hitbox
		
		att = getAttacker().getHitBox();
		width = (int) (att.getWidth() * 6.5);
		height = (int) (att.getHeight() / 4);
		hitboxY = (int) (att.getY() +  att.getHeight() / 4);
		hitboxX = (int) (att.getX() - width / 2 + att.getWidth() / 2);
		
		hitBox = new Rectangle(hitboxX, hitboxY, width, height);
		return attackAnimationUsed.getFrame() > 0  ? hitBox : new Rectangle(-1000, -1000, 0, 0);
	}

	@Override
	public boolean isBlocking() {
		return this.attackAnimationUsed.getFrame() == 0;
	}

	@Override
	public int getDuration() {
		return 750;
	}

	@Override
	public int getPower() {
		return 7;
	}

	@Override
	public void onHit(BasicFighter f, Rectangle2D Collision) {
		if (lastHit + windUp < currentTime) {
			if (f.isAttacking() && f.getCurrentAttack().isInterruptable()) {
			    f.getCurrentAttack().interrupt();
			}
			if (f.isParrying()) {
				f.parry(getPower());
			} else {
				f.hit(getPower());
			}
			lastHit = currentTime;
			this.hitSound.stop();	
			this.hitSound.play();	
		}
	}

	@Override
	public void trigger() {
		super.trigger();
		if (getAttacker().getY() > 0) {
			getAttacker().setYspeed(Math.abs(getAttacker().getYspeed()));
		}
		attackAnimationUsed = getAttacker().isForward() ? attackAnimation : attackAnimationBackward;
		lastHit = -250;
		windUp = WIND_UP;
	}
	
	@Override
	public void onRelease() {
	}
}
